package org.longgang.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.longgang.bean.Company;
import org.longgang.bean.CompanyInfo;
import org.longgang.bean.EducationExperience;
import org.longgang.bean.Position;
import org.longgang.bean.PracticalExperience;
import org.longgang.bean.User;
import org.longgang.service.CompanyInfoService;
import org.longgang.service.CompanyService;
import org.longgang.service.PositionService;
import org.longgang.tools.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CompanyController {

	@Autowired
	private CompanyService service;

	@Autowired
	private CompanyInfoService companyinfo;

	@Autowired
	private PositionService positions;

//	查询企业用户
	@RequestMapping("/getCompanyAll")
	public Object getCompanyAll(HttpServletRequest request) {
		Integer pagec = Integer.parseInt(request.getParameter("pagec"));
		Integer pages = Integer.parseInt(request.getParameter("pages"));
		Integer page=(pagec-1)*pages;
		
		List<Company> list = service.getCompanyAll(page,pages);
		
		ResponseData responseData = new ResponseData(3, list, "查询成功！");
		return responseData;
	}

//	用户注册
	@RequestMapping("/insertCompany")
	public Object insertCompany(HttpServletRequest request) {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String email = request.getParameter("email");

		Company company = new Company(name, password, email);
		service.insertCompany(company);
		Integer companyid = company.getCompanyId();

		companyinfo.insertCompanyInfo(new CompanyInfo(companyid));// 创建公司信息

		HttpSession session = request.getSession();
		session.setAttribute("cid", companyid);
		System.out.println(request.getSession().getId() + "  cid=" + request.getSession().getAttribute("cid"));

		ResponseData responseData = new ResponseData(1, company.getCompanyId(), "");
		return responseData;
	}

//	检查用户名
	@RequestMapping("/selectCompanyByName")
	public Object selectUserByName(HttpServletRequest request) {
		String name = request.getParameter("name");
		Integer i = service.selectCompanyByName(new Company(name, "", ""));

		ResponseData responseData = new ResponseData(1, i, "");
		return responseData;
	}

//	检查邮箱
	@RequestMapping("/selectCompanyByEmail")
	public Object selectUserByEmail(HttpServletRequest request) {
		String email = request.getParameter("email");
		Integer i = service.selectCompanyByEmail(new Company("", "", email));

		ResponseData responseData = new ResponseData(1, i, "");
		return responseData;
	}

//	登录
	@RequestMapping("/loginCompany")
	public Object loginCompany(HttpServletRequest request) {
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		Integer i = service.selectCompany(new Company("", password, email));

		HttpSession session = request.getSession();
		session.setAttribute("cid", i);
		System.out.println(request.getSession().getId() + "  cid=" + request.getSession().getAttribute("cid"));

		ResponseData responseData = new ResponseData(3, i, "");// 3 表示公司用户，用于前端识别
		return responseData;
	}

//	获取session的id
	@RequestMapping("/getCompanySession")
	public Object getCompanySession(HttpServletRequest request) {
		System.out.println(request.getSession().getId() + "  cid=" + request.getSession().getAttribute("cid"));

		ResponseData responseData = new ResponseData(3, request.getSession().getAttribute("cid"), "");
		return responseData;
	}

//	查询用户信息（根据Id）
	@RequestMapping("/selectCompanyById")
	public Object selectCompanyById(HttpServletRequest request) {
		Integer companyid = Integer.parseInt(request.getParameter("id"));
		Company company = service.selectCompanyById(new Company(companyid));
		ResponseData responseData = new ResponseData(3, company, "");
		return responseData;
	}

//	更新公司信息
	@RequestMapping("/updateCompanyInfo")
	public Object updateCompanyInfo(HttpServletRequest request) {
		String companyname = request.getParameter("companyname");
		String com = request.getParameter("com");
		String city = request.getParameter("city");
		String industry = request.getParameter("industry");
		String contact = request.getParameter("contact");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		String companyintroduction = request.getParameter("companyintroduction");

		Integer infoid = (Integer) request.getSession().getAttribute("cid");

		Integer i = companyinfo.updateCompanyInfo(
				new CompanyInfo(infoid, companyname, com, city, industry, contact, sex, phone, companyintroduction));

		ResponseData responseData = new ResponseData(3, i, "更新成功！");
		return responseData;
	}

//	查询当前登录的公司信息
	@RequestMapping("/selectCompanyInfoById")
	public Object selectCompanyInfoById(HttpServletRequest request) {
		Integer infoid = (Integer) request.getSession().getAttribute("cid");
		CompanyInfo selectCompanyInfoById = companyinfo.selectCompanyInfoById(new CompanyInfo(infoid));
		ResponseData responseData = new ResponseData(3, selectCompanyInfoById, "查询 成功！");
		System.out.println(selectCompanyInfoById);
		return responseData;
	}
	
//	查询公司信息
	@RequestMapping("/selectCompanyInfo")
	public Object selectCompanyInfo(HttpServletRequest request) {
		Integer infoid = Integer.parseInt(request.getParameter("cid"));
		CompanyInfo selectCompanyInfoById = companyinfo.selectCompanyInfoById(new CompanyInfo(infoid));
		ResponseData responseData = new ResponseData(3, selectCompanyInfoById, "查询 成功！");
		System.out.println(selectCompanyInfoById);
		return responseData;
	}

//	发布新职位
	@RequestMapping("/insertPosition")
	public Object insertPosition(HttpServletRequest request) {
		String position = request.getParameter("position");
		String workplace = request.getParameter("workplace");
		String classification = request.getParameter("classification");
		String department = request.getParameter("department");
		String salary = request.getParameter("salary");
		String edurequest = request.getParameter("edurequest");
		String experience = request.getParameter("experience");
		String statement = request.getParameter("statement");
		String email = request.getParameter("email");
		String deaddate = request.getParameter("deaddate");
		String b1 = request.getParameter("b1");
		String b2 = request.getParameter("b2");

		Integer companyid = (Integer) request.getSession().getAttribute("cid");

		Integer i = positions.insertPosition(new Position(companyid, position, workplace, classification, department,
				salary, edurequest, experience, statement, email, deaddate, b1, b2));

		ResponseData responseData = new ResponseData(3, i, "添加成功！");
		return responseData;
	}

//	更新职位信息
	@RequestMapping("/updatePosition")
	public Object updatePosition(HttpServletRequest request) {
		Integer positionid = Integer.parseInt(request.getParameter("positionid"));
		String position = request.getParameter("position");
		String workplace = request.getParameter("workplace");
		String classification = request.getParameter("classification");
		String department = request.getParameter("department");
		String salary = request.getParameter("salary");
		String edurequest = request.getParameter("edurequest");
		String experience = request.getParameter("experience");
		String statement = request.getParameter("statement");
		String email = request.getParameter("email");
		String deaddate = request.getParameter("deaddate");
		String b1 = request.getParameter("b1");
		String b2 = request.getParameter("b2");

		Integer companyid = (Integer) request.getSession().getAttribute("cid");

		Integer i = positions.updatePosition(new Position(positionid, companyid, position, workplace, classification,
				department, salary, edurequest, experience, statement, email, deaddate, b1, b2));

		ResponseData responseData = new ResponseData(3, i, "更新成功！");
		return responseData;
	}
	
//	查询当前用户的职位（传状态id查）
	@RequestMapping("/selectPosition")
	public Object selectPosition(HttpServletRequest request) {
		Integer status = Integer.parseInt(request.getParameter("status"));
		Integer companyid = (Integer) request.getSession().getAttribute("cid");
		
		List<Position> list = positions.selectPosition(new Position(0, companyid, status));

		ResponseData responseData = new ResponseData(3,list , "查询 成功！");
		return responseData;
	}

//	查询职位信息
	@RequestMapping("/selectPositionById")
	public Object selectPositionById(HttpServletRequest request) {
		Integer positionid = Integer.parseInt(request.getParameter("positionid"));
		Position selectPositionById = positions.selectPositionById(new Position(positionid));

		ResponseData responseData = new ResponseData(3, selectPositionById, "查询 成功！");
		System.out.println(selectPositionById);
		return responseData;
	}

//	改变职位状态
	@RequestMapping("/changePosition")
	public Object changePosition(HttpServletRequest request) {
		Integer positionid = Integer.parseInt(request.getParameter("positionid"));
		Integer status = Integer.parseInt(request.getParameter("status"));
		
		Integer i = positions.changePosition(new Position(positionid, status));

		ResponseData responseData = new ResponseData(3, i, "修改成功！");
		return responseData;
	}
	
//	首页分页查询最新职位
	@RequestMapping("/selectAllPosition")
	public Object selectAllPosition(HttpServletRequest request) {
		Integer status = Integer.parseInt(request.getParameter("status"));
		Integer pagec = Integer.parseInt(request.getParameter("pagec"));
		Integer pages = Integer.parseInt(request.getParameter("pages"));
		Integer page=(pagec-1)*pages;
		
		List<Position> list = positions.selectAllPosition(status,page,pages);

		ResponseData responseData = new ResponseData(3, list, "查询成功！");
		return responseData;
	}
	
//	首页分页查询企业
	@RequestMapping("/getAllCompanyInfo")
	public Object getAllCompanyInfo(HttpServletRequest request) {
		Integer pagec = Integer.parseInt(request.getParameter("pagec"));
		Integer pages = Integer.parseInt(request.getParameter("pages"));
		Integer page=(pagec-1)*pages;
		
		List<CompanyInfo> list = companyinfo.getAllCompanyInfo(page,pages);
		
		ResponseData responseData = new ResponseData(3, list, "查询成功！");
		return responseData;
	}
	
//	查询企业界面职位
	@RequestMapping("/selectCPosition")
	public Object selectCPosition(HttpServletRequest request) {
		Integer status = Integer.parseInt(request.getParameter("status"));
		Integer companyid = Integer.parseInt(request.getParameter("cid"));
		
		List<Position> list = positions.selectPosition(new Position(0, companyid, status));

		ResponseData responseData = new ResponseData(3,list , "查询 成功！");
		return responseData;
	}
	
//	查询计算企业职位数量
	@RequestMapping("/sumPosition")
	public Object sumPosition(HttpServletRequest request) {
		Integer status = Integer.parseInt(request.getParameter("status"));
		Integer companyid = Integer.parseInt(request.getParameter("cid"));
		
		Integer sumPosition = positions.sumPosition(new Position(0, companyid, status));

		ResponseData responseData = new ResponseData(3,sumPosition , "查询 成功！");
		return responseData;
	}
	
//	首页分页 查询职位总数量
	@RequestMapping("/sumAllPosition")
	public Object sumAllPosition(HttpServletRequest request) {
		Integer status = Integer.parseInt(request.getParameter("status"));
		
		Integer sumPosition = positions.sumAllPosition(new Position(0, 0, status));

		ResponseData responseData = new ResponseData(3,sumPosition , "查询 成功！");
		return responseData;
	}
	
//	根据职位名字模糊查询职位数量
	@RequestMapping("/sumSomePosition")
	public Object sumSomePosition(HttpServletRequest request) {
		Integer status = Integer.parseInt(request.getParameter("status"));
		String dat = request.getParameter("dat");
		
		Integer sumPosition = positions.sumSomePosition(status, dat);

		ResponseData responseData = new ResponseData(3,sumPosition , "查询 成功！");
		return responseData;
	}
	
//	根据职位名字模糊查询职位
	@RequestMapping("/selectSomePosition")
	public Object selectSomePosition(HttpServletRequest request) {
		Integer pagec = Integer.parseInt(request.getParameter("pagec"));
		Integer pages = Integer.parseInt(request.getParameter("pages"));
		Integer page=(pagec-1)*pages;
		Integer status = Integer.parseInt(request.getParameter("status"));
		String dat = request.getParameter("dat");
		
		List<Position> list = positions.selectSomePosition(status, dat, page, pages);

		ResponseData responseData = new ResponseData(3,list , "查询 成功！");
		return responseData;
	}
}
